function outkalman = BetaKalmanEstimate(params, process, rStock, rIndex)

    %---------------------------------------------------------------------------
    %kalman Filter process
    %--------------------------------------------------------------------------
    %Initial Conditions
    
    phi   = params(1);
    blong = params(2);
    alfa  = params(3);
    beta0 = params(4);
    SIGMA0 = params(5);
    std2betanu = params(6);
    std2betaeps = params(7);
    
    %Length of Time Series
    [n, m] = size(rStock);
    %Prediction Equations
    beta_t_1(1) = (1 - phi) * blong + phi * beta0;
    SIGMA_t_1(1) = (phi^2) * SIGMA0 + std2betanu;
    Et_1_Rt(1) = alfa + beta0.*(rIndex(1));
    %Updating equations
    vt(1) = rStock(1) - Et_1_Rt(1);
    ft(1) = (rIndex(1).^2).* (SIGMA0) + std2betaeps;
    %Matrix KALMAN GAIN 
    kt(1) = rIndex(1).* (SIGMA0)/ft(1);
    beta_t(1) = beta_t_1(1) + (kt(1).* vt(1));
    SIGMA_t(1) = SIGMA_t_1(1).* (1 - SIGMA_t_1(1) * (rIndex(1).^2) / ft(1));
    %MAX Verosomiglianza
    LOGMAx(1) = - log(2*pi) - log(abs(ft(1))) - (vt(1).^2)./ (ft(1));
    LOGverMAx(1) = LOGMAx(1)./ 2;
    stad(1) = vt(1)./ sqrt(ft(1));

    for i=2:n
        %Updating Equations    
        beta_t_1(i) = (1 - phi) * blong + phi * beta_t(i - 1);
        SIGMA_t_1(i) = (phi^2) * SIGMA_t(i-1) + std2betanu;
        Et_1_Rt(i) = alfa + beta_t_1(i).* (rIndex(i));
        %Updating equations
        vt(i) = rStock(i) - Et_1_Rt(i);
        ft(i) = (rIndex(i).^2).* (SIGMA_t_1(i)) + std2betaeps;
        %Matrix KALMAN GAIN 
        kt(i) = rIndex(i).* (SIGMA_t_1(i)) / ft(i);
        beta_t(i) = beta_t_1(i) + (kt(i).* vt(i));
        SIGMA_t(i) = SIGMA_t_1(i).* (1 - SIGMA_t_1(i) * (rIndex(i).^2) / ft(i));
        %MAX Verosomiglianza
        LOGMAx(i) = - log(2 * pi) - log(abs(ft(i))) - (vt(i).^2)./ (ft(i));    
        LOGverMAx(i) = LOGMAx(i)./2 + LOGverMAx(i - 1);
        stad(i) = vt(i)./ sqrt(ft(i));
    end
    outkalman = [beta_t_1', SIGMA_t_1', Et_1_Rt', vt', ft', kt', SIGMA_t', beta_t'];
end